package com.dfs;


import com.tree.TreeNode;


import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

public class test {
    //给你一棵二叉树的根节点root，请你返回层数最深的叶子节点的和
    public int deepestLeavesSum(TreeNode root){
        int res=0;
        Deque<TreeNode> deque=new LinkedList<>();
        deque.offer(root);
        while (!deque.isEmpty()){
            res=0;
            int size = deque.size();
            for (int i = 0; i < size; i++) {
                TreeNode node = deque.poll();
                res=res+node.val;
                if (node.left!=null){
                    deque.offer(node.left);
                }
                if (node.right!=null){
                    deque.offer(node.right);
                }
            }
        }
        return res;
    }
    int dep1=0,dep2=0;
    TreeNode fa1=null,fa2=null;
    public boolean isCousins(TreeNode root, int x, int y) {
        dfs(root,null,x,y,0);
        return dep1==dep2&&fa1!=fa2;

    }
    public void dfs(TreeNode root,TreeNode fa,int x,int y,int dep){
        if (root==null){
            return;
        }
        if (root.val==x){
            dep1=dep;
            fa1=fa;
        }
        if (root.val==y){
            dep2=dep;
            fa2=fa;
        }
        dfs(root.left,root,x,y,dep+1);
        dfs(root.right,root,x,y,dep+1);
    }

}
